package guo.mybatis;

import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserDao implements Closeable {
    private static final String SQLITE_DRIVER = "org.sqlite.JDBC";
    private static final String URL = "jdbc:sqlite:12-mybatis/src/main/resources/test.db";
    private static final String USER_NAME = "";
    private static final String PASSWORD = "";

    private Connection connection = null;
    private PreparedStatement statement = null;

    public UserDao(String sql){
        try {
            Class.forName(SQLITE_DRIVER);
            connection = DriverManager.getConnection(URL,USER_NAME, PASSWORD);
            statement = connection.prepareStatement(sql);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public PreparedStatement getStatement() {
        return statement;
    }

    @Override
    public void close() throws IOException {
        try {
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
